/**
* 操作面板界面
 * @author none
 */
class ZJHOperatePanelView extends how.module.View 
{
    public bipaiButton: how.Button;
    public genzhuButton: how.Button;
    public jiazhuButton: eui.Button;
    public jiazhuBar: eui.Group;
    public jiazhuNumGroup: eui.Group;
    public jiazhuNumLabel: how.Label;
    public jiazhuBeishu: number[] = [3, 5, 8, 10];
    public choumaRandomWidth: number = 250;
    public choumaRandomHeight: number = 50;
    private minJiazhuValue: number;//当前最小下注值
    private currentJiazhuValue: number;//当前加注值
    public handCardPanel: eui.Group;
    public qipaiArea: eui.Group;
    private handCardPanelDefaultPoint: egret.Point;
    /**
     * 加注条最大拖动距离（由编辑器设置）
     */
    public jiazhuBarMaxLength: number = 0;
    public start() {
        this.genzhuButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGenzhuButton, this);
        this.jiazhuButton.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onJiazhuButtonBegin, this);
        this.handCardPanel.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onHandCardPanelBegin, this);
        this.bipaiButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBipaiButton, this);
        this.jiazhuBar.visible = false;
        this.handCardPanelDefaultPoint = new egret.Point(this.handCardPanel.x, this.handCardPanel.y);
    }
    /*
     * 设置开始操作UI
     * */
    public setOperateUI(gameData: ZJHGameData): void {
        console.log(">>> ZJHOperatePanelView setOperateUI 设置开始操作UI",gameData.currentScore);
        this.genzhuButton.label = base.Utils.formatCurrency(gameData.currentScore);
        this.bipaiButton.label = base.Utils.formatCurrency(gameData.currentScore * 2);
        this.minJiazhuValue = gameData.currentScore;
        this.bipaiButton.visible = gameData.roundNum >= 3;
    }
    /**
     * 设置轮次
     */
    public setLunciUI(data: any): void {
        this.bipaiButton.visible = data.roundNum >= 3;
    }
    public setLookCardUI(gameData: ZJHGameData): void {
        this.genzhuButton.label = base.Utils.formatCurrency(gameData.currentScore);
        this.bipaiButton.label = base.Utils.formatCurrency(gameData.currentScore * 2);
        this.minJiazhuValue = gameData.currentScore;
    }
    private onGenzhuButton(): void {
        this.report("sendGenzhu");
    }
    private onBipaiButton(): void {
        this.report("sendBipai");
    }
    private startJiazhuY: number;
    private onJiazhuButtonBegin(event: egret.TouchEvent): void {
        this.jiazhuBar.visible = true;
        this.startJiazhuY = event.stageY;
        this.jiazhuNumGroup.bottom = 0;
        this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onJiazhuStageTouchMove, this);
        this.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.onJiazhuStageTouchEnd, this);
        this.computeJiazhuValue(0);
    }
    private onJiazhuStageTouchMove(event: egret.TouchEvent): void {
        var offsetY: number = this.startJiazhuY - event.stageY;
        offsetY = offsetY < 0 ? 0 : offsetY;
        offsetY = offsetY > this.jiazhuBarMaxLength ? this.jiazhuBarMaxLength : offsetY;
        this.jiazhuNumGroup.bottom = offsetY;
        this.computeJiazhuValue(offsetY);
    }
    private onJiazhuStageTouchEnd(event: egret.TouchEvent): void {
        this.jiazhuBar.visible = false;
        this.lastJiazhuIndex = 0;
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onJiazhuStageTouchMove, this);
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_END, this.onJiazhuStageTouchEnd, this);
        if (this.currentJiazhuValue > 0) {
            this.report("jiazhu", this.currentJiazhuValue);
        }
    }
    public onDestroy(): void {
        this.removeEvents();
    }
    private removeEvents(): void {
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onJiazhuStageTouchMove, this);
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_END, this.onJiazhuStageTouchEnd, this);
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onQipaiStageTouchMove, this);
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_END, this.onQipaiStageTouchEnd, this);
    }
    private lastJiazhuIndex: number = 0;
    private computeJiazhuValue(offset: number): void {
        var index: number = Math.floor(10 * offset / this.jiazhuBarMaxLength);
        var beishu: number;
        for (var i: number = 1; i <= this.jiazhuBeishu.length; i++) {
            if (index <= Math.floor(10 * i / this.jiazhuBeishu.length)) {
                beishu = this.jiazhuBeishu[i - 1];
                break;
            }
        }
        if (this.lastJiazhuIndex != index) {
            this.playEffect(this.lastJiazhuIndex > index);
        }
        this.lastJiazhuIndex = index;
        var gameData: ZJHGameData = <ZJHGameData>AppData.getInstance().gameData;
        this.currentJiazhuValue = gameData.lCellScore * beishu;
        this.jiazhuNumLabel.text = base.Utils.formatCurrency(this.currentJiazhuValue);
        this.currentJiazhuValue = offset > 0 ? this.currentJiazhuValue : 0;
    }
    private playEffect(reverse: boolean): void {
        if (!reverse) {
            (how.Application.currentScene as ZJHGameView).gameEffectPanel.playJiazhuChoumaEffect(
                this.jiazhuButton.localToGlobal(this.jiazhuButton.width / 2, this.jiazhuButton.height / 2),
                this.jiazhuNumGroup.localToGlobal(this.jiazhuNumGroup.width / 2, this.jiazhuNumGroup.height),
                this.localToGlobal(this.width / 2 - this.choumaRandomWidth / 2).x,
                this.localToGlobal(this.width / 2 - this.choumaRandomWidth / 2 + this.choumaRandomWidth).x,
                this.choumaRandomHeight
            );
        }
        else {
            (how.Application.currentScene as ZJHGameView).gameEffectPanel.playJiazhuChoumaEffect(
                this.jiazhuNumGroup.localToGlobal(this.jiazhuNumGroup.width / 2, this.jiazhuNumGroup.height / 2),
                this.jiazhuButton.localToGlobal(this.jiazhuButton.width / 2, 0),
                this.localToGlobal(this.width / 2 - this.choumaRandomWidth / 2).x,
                this.localToGlobal(this.width / 2 - this.choumaRandomWidth / 2 + this.choumaRandomWidth).x,
                this.choumaRandomHeight
            );
        }
    }
    private startQipaiPoint: egret.Point = new egret.Point();
    private onHandCardPanelBegin(event: egret.TouchEvent): void {
        if (this.visible) {//如果可以操作
            this.qipaiArea.visible = true;
            this.startQipaiPoint.x = event.stageX;
            this.startQipaiPoint.y = event.stageY;
            this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onQipaiStageTouchMove, this);
            this.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.onQipaiStageTouchEnd, this);
        }
    }
    private onQipaiStageTouchMove(event: egret.TouchEvent): void {
        var offsetX: number = event.stageX - this.startQipaiPoint.x;
        var offsetY: number = event.stageY - this.startQipaiPoint.y;
        this.handCardPanel.x = this.handCardPanelDefaultPoint.x + offsetX;
        this.handCardPanel.y = this.handCardPanelDefaultPoint.y + offsetY;
    }
    private onQipaiStageTouchEnd(event: egret.TouchEvent): void {
        this.handCardPanel.x = this.handCardPanelDefaultPoint.x;
        this.handCardPanel.y = this.handCardPanelDefaultPoint.y;
        this.qipaiArea.visible = false;
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onQipaiStageTouchMove, this);
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_END, this.onQipaiStageTouchEnd, this);
        var qipaiAreaBg: eui.Image = this.qipaiArea.getChildAt(0) as eui.Image;
        var pos: egret.Point = this.qipaiArea.globalToLocal(event.stageX, event.stageY);
        var colors: number[] = qipaiAreaBg.texture.getPixel32(pos.x, pos.y);
        if (colors[0] == 255 && colors[1] == 255 && colors[2] == 255 && colors[3] == 26) {//区域内
        }
        else {//区域外，执行弃牌
            this.report("qipai");
        }
    }
    private onOperateOver(): void 
    {
        this.visible = false;
        this.removeEvents();
    }
    private onRoundOver(): void {
        this.visible = false;
        this.removeEvents();
    }
}